package com.yy.auth.server.config.filter;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
import org.springframework.security.web.savedrequest.RequestCache;
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;

/**
 * @Description 授权请求过滤器
 * @Date 2025/10/10 下午1:56
 * @Author yanglin
 **/
@Configuration
public class AuthRequestFilterConfig {

    private static final Logger logger1 = LoggerFactory.getLogger(AuthRequestFilterConfig.class);

    @Bean
    public OncePerRequestFilter authorizationRequestFilter() {
        return new OncePerRequestFilter() {
            private final RequestCache requestCache = new HttpSessionRequestCache();

            @Override
            protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
                                            FilterChain filterChain) throws ServletException, IOException {
                // 只处理授权端点请求
                if (request.getRequestURI().equals("/oauth2/authorize")) {
                    requestCache.saveRequest(request, response);
                    logger1.debug("[SSO-AUTH-SERVER-FILTER] OncePerRequestFilter getRequestURI: {}", request.getRequestURI());
                }
                filterChain.doFilter(request, response);
            }
        };
    }
}
